﻿using Adnc.Application.Shared.Dtos;
using Adnc.Application.Shared.Services;
using Adnc.Infra.IRepositories;
using Adnc.Shared.Consts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WYT.Sys.Application.Contracts.Dtos.Encyclopedia;
using WYT.Sys.Application.Contracts.Services;
using WYT.Sys.Repository.Entities;

namespace WYT.Sys.Application.Services
{
    public class EncyclopediaAppService : AbstractAppService, IEncyclopediaAppService
    {
        private readonly IEfRepository<EncyclopediaGroup> _groupEfRepository;
        private readonly IEfRepository<EncyclopediaContent> _contentEfRepository;

        public EncyclopediaAppService(IEfRepository<EncyclopediaGroup> groupEfRepository
            , IEfRepository<EncyclopediaContent> contentEfRepository)
        {
            _groupEfRepository = groupEfRepository;
            _contentEfRepository = contentEfRepository;
        }

        /// <summary>
        /// 获取问题
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<PageModelDto<EncyclopediaContentDto>>> GetContentList(SearchPagedDto<long> input)
        {
            var iszh = input.IsZh();
            var count = await _contentEfRepository.CountAsync(x => x.GroupId == input.Value && x.IsDelete == false);
            var data = await _contentEfRepository.Where(x => x.GroupId == input.Value && x.IsDelete == false)
                .Skip(input.PageSize * (input.PageIndex - 1))
                .Take(input.PageSize)
                .Select(x => new EncyclopediaContentDto {
                    Id = x.Id,
                    Question = iszh ? x.Question : x.UighurQuestion,
                    Answer  = iszh ? x.Answer : x.UighurAnswer,
                })
                .ToListAsync();
            return new PageModelDto<EncyclopediaContentDto>(input.PageIndex,input.PageSize,data,count);
        }

        /// <summary>
        /// 获取分组
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<List<EncyclopediaGroupDto>>> GetGroupList(string language)
        {
            var iszh = language.Equals(LanguageConsts.ZH);
            return await _groupEfRepository.Where(x => x.IsDeleted == false)
                .OrderBy(x => x.Sort)
                .Select(x => new EncyclopediaGroupDto
                {
                    Id = x.Id,
                    Name = iszh ? x.Name : x.Name,
                    Describe = iszh ? x.Describe : x.UighurDescribe,
                    Basemap = x.Basemap
                }).ToListAsync();
        }
    }
}
